package com._520it.day1._04_dql;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.junit.Test;

public class DQLDemo {
	
	//查询id为10的商品
	@Test
	public void testQuerySingle() throws Exception {
		//加载注册驱动
		Class.forName("com.mysql.jdbc.Driver");
		//获取连接对象
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "");
		// 创建语句对象
		Statement st = conn.createStatement();
		//执行sql
		String sql="SELECT * FROM product WHERE id=10";
		ResultSet rs=st.executeQuery(sql);
		//将返回的值取出
		if(rs.next()){
			long id = rs.getLong(1);
			System.out.println(id);
			String name = (String) rs.getObject("productName");
			System.out.println(name);
		}
		//释放资源
		rs.close();
		st.close();
		conn.close();
	}
	
	/**
	 * 查询所有的商品
	 * @throws Exception
	 */
	@Test
	public void testQueryAll() throws Exception {
		//加载注册驱动
				Class.forName("com.mysql.jdbc.Driver");
				//获取连接对象
				Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "");
				// 创建语句对象
				Statement st = conn.createStatement();
				//执行sql
				String sql="SELECT * FROM product";
				ResultSet rs=st.executeQuery(sql);
				//将返回的值取出
				while(rs.next()){
					long id = rs.getLong(1);
					System.out.println(id);
					String name = (String) rs.getObject("productName");
					System.out.println(name);
				}
				//释放资源
				rs.close();
				st.close();
				conn.close();
	}
}
